package online.sh.cn.fy.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.SqlUtil;

/**
 * Servlet implementation class fycaption
 */
@WebServlet("/fycaption")
public class fycaption extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public fycaption() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession();

		// 判断是否传入个人信息，如果传入则获取用户公众号信息
		String openid=CookieUtil.getCookieValueByName(request, "openid");//"oSQHvshrOHDijd23lPmghqQNQ4Dw";//"oSQHvshrOHDijd23lPmghqQNQ4Dw";//"oMPIqxMDiBs2VW8PXl_d5rDdruz8";//getOpenId(code);//
		
		System.out.println("OpenIdForCookieUtil------------------->" + openid);
		 //0代表young粉用户、1代表非young粉
		String check=CookieUtil.getCookieValueByName(request, "uuid");
		try {
		     openid=DES3Util.decrypt(openid);
		     check=DES3Util.decrypt(check);
		    } catch (Exception e) {
		     log(e.getMessage());
		    }
		System.out.println("OpenIdForCookieUtilDecrypt------------------->" + openid);

		

		
		System.out.println("获young粉用户(0代表young粉用户、1代表非young粉)——》" + check);
		System.out.println("获取到个人公众号信息openid——》" + openid);
		
		session.setAttribute("check", check);
		
		SqlUtil sqlutil= new SqlUtil();
		Map<String,String> result=null;
		if(null!=openid && !"".equals(openid)){
			List<String> rs = sqlutil.getfyCaptionshnamefromOpenId(openid);
			for(String shname : rs){
				result = sqlutil.getfyCaption1(shname);
	        }

		}
		if(null!=result && result.size()>0){
			session.setAttribute("cpname", result.get("cpname"));
	        session.setAttribute("cplevel", result.get("cplevel"));
	        session.setAttribute("cpqq", result.get("cpqq"));
	        
	        response.sendRedirect("./call.jsp");
		}else{
			response.sendRedirect("./call_notYoung.html");
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		SqlUtil sqlutil= new SqlUtil();
		List<String> rs = sqlutil.getfyCaptionshname();
		JSONArray json = new JSONArray();
        for(String pLog : rs){
            JSONObject jo = new JSONObject();
            jo.put("shname", pLog);
             
            json.add(jo);
        }
		
		ResponseUtils.renderJson(response, json.toString());


		
	}
 
       
}
